package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class workaddstuclass
 */
@WebServlet("/workaddstuclass")
public class workaddstuclass extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public workaddstuclass() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String stuid = request.getParameter("stuid");
		String stuname=request.getParameter("stuname");
		String centername=request.getParameter("centername");
		String classname=request.getParameter("classname");
		//第一步，先查询这个班级是否还有人数可以添加进去
		String find1="select stunumrest from class where classname='"+classname+"' and centername='"+centername+"'";
		//第二步，判断这个学生是否已经添加了这个班级,由于一个学生可以选择好几个班级
		//所已在STU-CLASS表格中查找数据的时候，在结果集中不断遍历直到找到是否在结果
		//中存在现在要添加的班级
		String find2="select * from stu_class where stuid='"+stuid+"'";
		//判断人数是否达标
		Jdbc j1 =new Jdbc();
		try {
			j1.Jdbc_Q2(find1);
			j1.rs.next();
			String restnum =  j1.rs.getString("stunumrest");
		    
			if(restnum.equals("0")) {
				request.getRequestDispatcher("showclassrestnum.jsp").forward(request,response);
				return;
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//判断在STU_CLASS中是否已经添加了这个班级（需要用中心号和班级号共同确定一个班级）;
		Jdbc j2 =new Jdbc();
		try {
			j2.Jdbc_Q2(find2);
			String rsclass;
			String rscenter;
			while(j2.rs.next()) {
				rsclass=j2.rs.getString("classname");
				System.out.println(rsclass);
				rscenter=j2.rs.getString("centername");
				System.out.println(rscenter);
				if(rsclass.equals(classname)&&rscenter.equals(centername)) {//表示这个班级已经选过
				request.getRequestDispatcher("showclasschoosed.jsp").forward(request,response);
				return;
				}
			}
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//若经过了上面两个判断后，说明班级可选并且没有选过，则将学生的选择（中心号，班级号，学生姓名，学生ID）添加到STU-CLASS表格中去
		//同时将CLASS表中的这个班级的可选人数减一
		//并且将此学生添加到这个班级的考勤表中去考勤表名为 centername+classname+"check"
		String checktable=centername+classname+"check";
		String insert1="insert into stu_class(centername,classname,chinesename,stuid) values('"+centername+"','"+classname+"','"+stuname+"','"+stuid+"')";
		String insert2="update class set stunumrest=stunumrest-1 where classname='"+classname+"' and centername='"+centername+"'" ;
		String insert3="insert into"+" "+checktable+"(stuid,stuname,classname,centername,total,absence) values('"+stuid+"','"+stuname+"','"+classname+"','"+centername+"','0','0')";
		
		try {
			Jdbc j3 = new Jdbc(insert1);
			Jdbc j4 = new Jdbc(insert2);
			Jdbc j5 = new Jdbc(insert3);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//一旦选好课程就将这个学生添加到check_info中，如果还没有添加过的话
		//即使已经存在这个学生，那么就会插入失败，不会对数据库作出任何改变
		String insert4 = "insert into check_info(stuid,stuname,count,absence) values('"+stuid+"','"+stuname+"','1','0')";
		System.out.println("check_info开始啦");
		System.out.println(insert4);
		try {
			Jdbc j6 = new Jdbc(insert4);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
	}

}
